﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Abigail
{
    class Popup
    {
        /// <summary>
        /// Displays a popup saying that the function is not implemented.
        /// </summary>
        public static void FunctionNotImplemented()
        {
            Popup.Information("Not quite there yet.", "Not implemented");
        }

        /// <summary>
        /// Displays a popup with given information
        /// </summary>
        /// <param name="message">The message inside the popup</param>
        /// <param name="caption">The caption on top of the poup</param>
        /// <param name="icon">A text definition of the icon inside the popup</param>
        public static void Message(string message, string caption, string icon)
        {
            MessageBox.Show(message, caption, MessageBoxButtons.OK, GetIconFromString(icon));
        }
        /// <summary>
        /// Displays a popup with given information and prompt user for answer.
        /// </summary>
        /// <param name="message">The message inside the popup</param>
        /// <param name="caption">The caption on top of the poup</param>
        /// <param name="icon">A text definition of the icon inside the popup</param>
        /// <returns>True if user answers OK, false otherwise.</returns>
        public static bool QuestionOKCancel(string message, string caption, string icon)
        {
            DialogResult result = MessageBox.Show(message, caption, MessageBoxButtons.OKCancel, GetIconFromString(icon));
            if (result == DialogResult.OK)
                return true;
            else
                return false;
        }

        /// <summary>
        /// Displays a popup with given information and prompt user for answer.
        /// </summary>
        /// <param name="message">The message inside the popup</param>
        /// <param name="caption">The caption on top of the poup</param>
        /// <param name="icon">A text definition of the icon inside the popup</param>
        /// <returns>True if user answers Yes, false otherwise.</returns>
        public static bool QuestionYesNo(string message, string caption, string icon)
        {
            DialogResult result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, GetIconFromString(icon));
            if (result == DialogResult.Yes)
                return true;
            else
                return false;
        }

        /// <summary>
        /// Displays a popup with given information.
        /// </summary>
        /// <param name="message">The message inside the popup</param>
        /// <param name="caption">The caption on top of the poup</param>
        public static void Error(string message, string caption)
        {
            MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        /// <summary>
        /// Displays a popup with the exception as a string.
        /// </summary>
        /// <param name="e">The exception to display</param>
        public static void Error(Exception e)
        {
            Popup.Error(e.ToString(), e.GetType().ToString());
        }

        /// <summary>
        /// Displays a popup with given information.
        /// </summary>
        /// <param name="message">The message inside the popup</param>
        public static void Error(string message)
        {
            MessageBox.Show(message, "Fel", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        /// <summary>
        /// Displays a popup with given information.
        /// </summary>
        /// <param name="message">The message inside the popup</param>
        /// <param name="caption">The caption on top of the poup</param>
        public static void Information(string message, string caption)
        {
            MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        /// <summary>
        /// Displays a popup with given information.
        /// </summary>
        /// <param name="message">The message inside the popup</param>
        public static void Information(string message)
        {
            MessageBox.Show(message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        /// <summary>
        /// Displays a popup with given information.
        /// </summary>
        /// <param name="message">The message inside the popup</param>
        /// <param name="caption">The caption on top of the poup</param>
        public static void Warning(string message, string caption)
        {
            MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        /// <summary>
        /// Displays a popup with given information.
        /// </summary>
        /// <param name="message">The message inside the popup</param>
        public static void Warning(string message)
        {
            MessageBox.Show(message, "Varning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        /// <summary>
        /// Tells the user that connection to database failed and asks for action.
        /// </summary>
        /// <returns>True if user answers Retry, false otherwise.</returns>
        public static bool DatabaseFailRetryCancel()
        {
            DialogResult result =  MessageBox.Show("Kunde inte ansluta till databasen", "Fel", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.Retry)
                return true;
            else
                return false;
        }

        /// <summary>
        /// Returns the icon object for given string. Falls back in none if no match.
        /// </summary>
        /// <param name="iconString">String representation of the icon</param>
        /// <returns>The icon for the string.</returns>
        private static MessageBoxIcon GetIconFromString(string iconString)
        {
            if (iconString == "asterisk")
                return MessageBoxIcon.Asterisk;
            else if (iconString == "error")
                return MessageBoxIcon.Error;
            else if (iconString == "exclamation")
                return MessageBoxIcon.Exclamation;
            else if (iconString == "hand")
                return MessageBoxIcon.Hand;
            else if (iconString == "information")
                return MessageBoxIcon.Information;
            else if (iconString == "question")
                return MessageBoxIcon.Question;
            else if (iconString == "stop")
                return MessageBoxIcon.Stop;
            else if (iconString == "warning")
                return MessageBoxIcon.Warning;
            else
                return MessageBoxIcon.None;
        }
    }
}
